package com.sztest.jayjun.adapter;

/*
 *  @项目名：  pro_8195_ 
 *  @包名：    com.chomp.common.adapter
 *  @文件名:   AIListRecycleViewAdapter
 *  @创建者:   okmin
 *  @创建时间:  2019/1/9 17:54
 *  @描述：    TODO
 */

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.sztest.jayjun.R;
import com.sztest.jayjun.bean.DebugItemBean;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class DebugRecycleViewAdapter extends RecyclerView.Adapter<DebugRecycleViewAdapter.ViewHolder> {
    List<DebugItemBean> mData;
    private AIListI mListener;
    private int mType=1;

    public DebugRecycleViewAdapter(int type) {
        mType = type;

        mData = new ArrayList<>();
    }

    public List<DebugItemBean> getData() {
        return mData;
    }

    public void setData(List<DebugItemBean> data) {
         mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int res=R.layout.item_debug1;
        if (mType==2) {
            res=R.layout.item_debug2;
        }else if (mType==3) {
            res=R.layout.item_debug3;
        }
        View inflate = View.inflate(parent.getContext(), res, null);
        return new ViewHolder(inflate);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        DebugItemBean bean = mData.get(position);
        holder.mTvPosition.setText(bean.getPosition());
        if (bean.getType()==3) {
            holder.mTvNum.setText(getOneDecimal(bean.getNum())+"Hz");
        }else if (bean.getType()==2){
            holder.mTvNum.setText(getOneDecimal(bean.getNum())+"s");
        }else{
            holder.mTvNum.setText("-"+((int)bean.getNum()));
        }

        holder.mIvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null) {
                    mListener.onAddClick(position);
                }
            }
        });
        holder.mIvDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null) {
                    mListener.onDownClick(position);
                }
            }
        });
    }





    public void setAIListI(AIListI listener) {
        mListener = listener;
    }

    public interface AIListI {

        void onAddClick(int position);

        void onDownClick(int position);

    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mTvPosition;
        private ImageView mIvDown;

        private TextView mTvNum;
        private ImageView mIvAdd;

        public ViewHolder(View itemView) {
            super(itemView);
            mTvPosition=itemView.findViewById(R.id.tv_positon);
            mIvDown=itemView.findViewById(R.id.iv_down);

            mTvNum=itemView.findViewById(R.id.tv_num);
            mIvAdd=itemView.findViewById(R.id.iv_add);
        }
    }

    private double getOneDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#.0");

        String yearString = dFormat.format(num);

        Double temp = Double.valueOf(yearString);

        return temp;

    }
}
